package cn.kin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.kin.pojo.Notice;
import cn.kin.pojo.Staff;
import cn.kin.service.NoticeService;
import cn.kin.tools.PageSupport;
import cn.kin.tools.ShiroDbRealm;

@Controller
@RequestMapping("notice")
public class NoticeController {

	@Resource
	private NoticeService noticeBiz;
	
	@RequestMapping("mian")
	public String noticeMian() {
		System.out.println("notice---------mian");
		return "common/notice";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Object noticeList(@RequestParam(value="pageNo",required=false)Integer pageNo) {
		Map<String, Object> resultMap = new HashMap<>();
		System.out.println("notice---------list");
		//页码属性
		PageSupport page=new PageSupport();
		page.setPageSize(10);
		page.setTotalCount(noticeBiz.getCount());	
		//当前页数
		if(pageNo==null) {
			pageNo=1;
		}else if(pageNo<1) {
			pageNo=1;
		}else if(pageNo>page.getTotalPageCount()) {
			pageNo=page.getTotalPageCount();
		}
		page.setCurrentPageNo(pageNo);
				
		List<Notice> nList=noticeBiz.getNoticeList(page.getCurrentPageNo(),page.getPageSize());
		
		resultMap.put("nList",nList);
		resultMap.put("page",page);
		
		return resultMap;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public Object addNotice(Notice notice,HttpSession session) {
		Map<String, Object> resultMap = new HashMap<>();
		notice.setStaff((Staff)session.getAttribute(ShiroDbRealm.SESSION_USER_KEY));
		if(noticeBiz.addNotice(notice)) {
			resultMap.put("msg", "success");
		}else {
			resultMap.put("msg", "error");
		}
		return resultMap;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Object delNotice(Integer id) {
		Map<String, Object> resultMap = new HashMap<>();
		if(noticeBiz.delNotice(id)) {
			resultMap.put("msg", "success");
		}else {
			resultMap.put("msg", "error");
		}
		return resultMap;
	}
}
